<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;  
use Illuminate\Support\Facades\Log;  
use App\Models\OrderColor;
use App\Models\Requirement;
use App\Models\Project;
use App\Models\Remind;
use App\Models\Feature;
use App\Models\Module;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Redis;

class RemindsController extends Controller
{
    
    public function store_remind(Request $request,Remind $remind)
    { 
        $remind->admin_user_id =  $request->now_admin_user_id;
        $remind->remind_admin_user_id = $request->remind_admin_user_id;
        $remind->remind_admin_user_requirement_id = $request->admin_user_requirement_id;
        $remind->remind_content = $request->content;
        $remind->save();

        return redirect("/");

       
    }

    public function ajax_reminds(Request $request,$adminUserId){
        $reminds = Remind::where([
            ['remind_admin_user_id',$adminUserId],
            ['remind_status',0]
        ])->orderBy('created_at','DESC')->with('adminUser',
            'adminUserRequirement',
            'adminUserRequirement.requirement',
            'adminUserRequirement.requirement.module',
            'adminUserRequirement.requirement.module.project')->get();

        $html ='';

        // 将提醒转换为数组并返回 JSON 响应  
        $remindArray = $reminds->map(function ($remind) {
            return "<div class='text-with-line' id='reply_remind".$remind->id."'>发送人：".$remind->adminUser->name.'，<br>任务：'.$remind->adminUserRequirement->requirement->module->project->name.'-'.
            $remind->adminUserRequirement->requirement->module->name.'-'.
            $remind->adminUserRequirement->requirement->type.'-'.
            $remind->adminUserRequirement->requirement->name.'<br>提醒内容：'.$remind->remind_content.'<br><button style="float:right;" class="received-button" data-reminder-id="'.$remind->id.'">收到</button></div>';  
        })->all();  

        return response()->json(['data' => $remindArray,'remind_admin_user_id'=>$adminUserId]); 
    }

    public function ajax_received(Request $request,$reminderId){
        $remind = Remind::find($reminderId);
        if ($remind) {  
            $remind->remind_status = 1;  
            $remind->save(); // 不要忘记保存更改  
      
            // 返回一个成功的HTTP响应  
            return response()->json(['status' => 'ok'], 200);  
        } else {  
            // 如果找不到提醒，返回一个错误响应  
            return response()->json(['error' => 'Reminder not found'], 404);  
        }  
    }
}
